// pages/home/home.js
//获取应用实例
var app = getApp();
var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');
Page({
  data: {
    search: {
      searchValue: '',
    },
    searchResult: [],
    title: '新闻列表',
    postsList: [],
    hidden: false,
    page: 1,
    tab: 'top'

  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数

    //访问聚合数据的网络接口
    this.fetchData();
  
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  //输入内容时
  searchActiveChangeinput: function (e) {
    const val = e.detail.value;

    this.setData({
      'search.showClearBtn': val != '' ? true : false,
      'search.searchValue': val
    })

  },
  redictDetail:function(e){
    console.log('我要看详情');
    var id = e.currentTarget.id;
    console.log(id);
    var url = '../detail/detail?id='+id;
    console.log(url);
    wx.navigateTo({
      url: url      
    })
  },


  fetchData:function(data){

    var self = this;
    self.setData({
      hidden: false
    });
    if (!data) data = {};
    //if (!data.tab) data.tab = "top";
    self.setData({
      postsList: []
    });
    wx.request({
      url: Api.getNewsList(data),
      success: function (res) {
        console.log(res);
        self.setData({
          
          postsList: self.data.postsList.concat(res.data.result.data.map(function (item) {
            //item.last_reply_at = util.getDateDiff(new Date(item.date));
            return item;
          }))
        });
        setTimeout(function () {
          self.setData({
            hidden: true
          });
        }, 300);
      }
    });
  }
})